import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:school_assistant_m/app/routes/app_pages.dart';
import 'package:school_assistant_m/utils/pp_template_list_view.dart';
import '../controllers/course_controller.dart';

class CourseView extends GetView<CourseController> {
  //  使用Get.put()实例化待提交类，使其对当下的所有子路由可用。
  // CourseController courseController = Get.put(CourseController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        // backgroundColor: Colors.red,
        title: Text(
          '课程',
          style: TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.w600,
          ),
        ),
        actions: <Widget>[
          PopupMenuButton(
            itemBuilder: (BuildContext context) {
              return getPopupList();
            },
            offset: Offset(0.0, 55.0),
            elevation: 3.0,
            // color: Colors.lightBlue,
            icon: Icon(Icons.add_circle_outline, size: 25, color: Colors.black),
            onSelected: (value) async {
              print('点击的是$value');
              Get.toNamed(Routes.ADD_COURSE);
            },
          ),
          SizedBox(width: 10.0),
        ],
      ),
      body: ConstrainedBox(
        constraints: BoxConstraints.expand(),
        child: Stack(
          alignment: Alignment.center,
          children: <Widget>[
            controller.obx((state) => Center(
              child: PpTemplateListView(
                title: 'course',
                title1: '老师',
                desc1: 'teacher',
                code: 'courseDesc',
                title2: '上课时间',
                desc2: 'schoolTime',
                title3: '下课时间',
                desc3: 'finishTime',
                dataForm: controller.dataForm,
                onPressed: (Map<dynamic, dynamic> value) {  },
                onTap: (id) {
                  print(id);
                  controller.getScoreById(id);
                },
                dropdown: () {
                  print('下拉刷新');
                  controller.getScoreList();
                },
                pullup: () {
                  print('上拉加载');
                  controller.getScoreList(); },
              ),
            ),
              onError: (err) {
                return Text('$err');
              },
              onLoading: Container(
                width: double.infinity,
                height: double.infinity,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    CircularProgressIndicator(),
                    SizedBox(height: 10),
                    Text(
                      "疯狂加载中...",
                      style: TextStyle(color: Colors.blue, fontSize: 16),
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      )
    );
  }
  List<PopupMenuEntry> getPopupList() {
    if (controller.isAdmin||controller.role.toString()=='老师') {
      return [
        PopupMenuItem(
          child: _buildPopupMenuItem(Icons.book, "添加课程"),
          value: 'add_course',
        ),
        // PopupMenuDivider(height: 1.0),
      ];
    } else {
      return [
        PopupMenuItem(
          child: _buildPopupMenuItem(Icons.book, "挑选课程"),
          value: 'pick_course',
        ),
        // PopupMenuDivider(height: 1.0),
      ];
    }
  }

  _buildPopupMenuItem(IconData iconName, String title) {
    return Column(
      children: [
        Row(
          children: <Widget>[
            Icon(iconName, size: 22.0, color: Colors.black),
            SizedBox(width: 12.0),
            Text(title, style: TextStyle(color: Colors.black)),
          ],
        ),
      ],
    );
  }
}
